package com.burst.reptile.modular.zp.service.impl;

import com.burst.reptile.common.constant.Constants;
import com.burst.reptile.common.utils.SecurityUtils;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.framework.redis.RedisCache;
import com.burst.reptile.modular.zp.entity.ZpCommSup;
import com.burst.reptile.modular.zp.mapper.ZpCommSupMapper;
import com.burst.reptile.modular.zp.service.IZpCommSupService;

import lombok.RequiredArgsConstructor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 评论点赞时间 服务实现类
 * </p>
 *
 * @author wujin
 * @since 2020-06-30
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZpCommSupServiceImpl extends ServiceImpl<ZpCommSupMapper, ZpCommSup> implements IZpCommSupService {

	private final RedisCache redisCache;
	
	@Override
	public int praise(Long id) {
		Long loginUserId = SecurityUtils.getLoginUserId();
		String key = Constants.PRAISE_KEY + loginUserId + "_" + id;
		// 是否重复点赞
		Object cacheObject = redisCache.getCacheObject(key);
		if(ToolUtil.isNotEmpty(cacheObject)) {
			return 0;
		}
		int count = this.count(new QueryWrapper<ZpCommSup>().eq("cid", id).eq("user_id", loginUserId));
		if(count == 0) {
			redisCache.setCacheObject(key, id);
			ZpCommSup commSup = new ZpCommSup();
			commSup.setCid(id);
			commSup.setUserId(loginUserId);
			int insert = this.baseMapper.insert(commSup);
			redisCache.deleteObject(key);
			return insert;
		}
		return 0;
	}

	@Override
	public int unpraise(Long id) {
		Long loginUserId = SecurityUtils.getLoginUserId();
		return this.baseMapper.delete(new QueryWrapper<ZpCommSup>().eq("cid", id).eq("user_id", loginUserId));
	}

	@Override
	public int wxpraise(Long id, Long userId) {
		int count = this.count(new QueryWrapper<ZpCommSup>().eq("cid", id).eq("user_id", userId));
		if(count == 0) {
			ZpCommSup commSup = new ZpCommSup();
			commSup.setCid(id);
			commSup.setUserId(userId);
			int insert = this.baseMapper.insert(commSup);
			return insert;
		}
		return 0;
	}

	@Override
	public int wxunpraise(Long id, Long userId) {
		return this.baseMapper.delete(new QueryWrapper<ZpCommSup>().eq("cid", id).eq("user_id", userId));
	}

}
